package com.tanhua.dubbo.api.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.dubbo.pojo.User;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import com.tanhua.dubbo.mapper.UserMapper;

/**
 * 声明是一个dubbo服务
 */

@DubboService(version = "1.0.0")
public class UserApiImpl implements UserApi {

    @Autowired //注入UserMapper对象
    private UserMapper userMapper;

    @Override
    public User queryByMobile(String mobile) {
        //↓ QueryWrapper为提供的,相当于构建MySQL语句
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        //↑ 相当于在mobile字段中查找等于传入值(mobile)的数据行
        //返回一个User对象,userMapper的selectOne方法对queryWrapper(等价于SQL语句)进行查询
        return this.userMapper.selectOne(queryWrapper);
    }

    @Override
    public User queryById(Long id) {
        return this.userMapper.selectById(id);
    }

    /**
     * @param mobile
     * @return id
     * 新建对象,插入手机号和密码(密码设置为默认值常量)
     * MyBatisPlus中userMapperInsert方法中自动主键回填
     */
    @Override
    public Long save(String mobile) {
        User user = new User();
        user.setMobile(mobile);
        user.setPassword("123456");
        this.userMapper.insert(user);
        return user.getId();
    }
}
